<?php
namespace Wap\Controller;
use Think\Controller;
use Think\Model;

class EventController extends Controller
{
    //private $appid = 'wxcc0fc9299ebd072b';
    private $appid = 'wxf0c385cdc1da932a';
    //private $appsecret = 'b1039c948dfbf76c6f3c124ad86af1bb';
    private $appsecret = '02282bbb9535237e2d449ba9322abe2b';

    public function index()
    {
        $this->display();
    }

    public function refereeOrganiztion()
    {
        $event = I( 'get.event', -1, '' );

        $conditions = array(
            'event_id' => $event
        );

        $result = M( 'organiztion' )
            ->where( $conditions )
            ->order( 'score desc, praise desc' )
            ->select();

        $this->ajaxReturn( $result );
    }

    public function refereeOrganiztionItem()
    {
        $event = I( 'get.event', -1, '' );
        $organiztion = I( 'get.organiztion', -1, '' );

        $conditions = array(
            'esm_event_item.event_id' => $event,
            'esm_organiztion.id' => $organiztion
        );

        $columns = array(
            'esm_event_item.id' => 'id',
            'esm_event_item.event_id' => 'event',
            'esm_event_item.name' => 'name',
            'esm_organiztion.id' => 'organiztion',
            'esm_organiztion_item.id' => 'scored'
        );

        $result = M( 'event_item' )
            ->join( 'LEFT JOIN esm_organiztion ON esm_event_item.event_id = esm_organiztion.event_id' )
            ->join( 'LEFT JOIN esm_organiztion_item ON esm_organiztion.id = esm_organiztion_item.organiztion_id' )
            ->field( $columns )
            ->where( $conditions )
            ->group( 'esm_event_item.id' )
            ->select();

        $this->ajaxReturn( $result );
    }

    public function scoreInc()
    {
        $event = I( 'post.event', -1, '' );
        $event_item = I( 'post.event_item', -1, '' );
        $organiztion = I( 'post.organiztion', -1, '' );
        $score = I( 'post.score', 0, '' );

        $scored = D( 'event' )->scoreInc( $event, $event_item, $organiztion, $score );

        $result = array(
            'success' => $scored,
            'updated' => null
        );

        if( !$scored )
        {
            $this->ajaxReturn( $result );
            return;
        }

        $result['updated'] = D( 'event' )->rank( $organiztion );

        $this->ajaxReturn( $result );
    }

    public function praiseInc()
    {
        $event = I( 'post.event', -1, '' );
        $organiztion = I( 'post.organiztion', -1, '' );

        $result = array(
            'success' => false,
            'openid' => null,
            'status' => false,
            'rule' => array(),
            'updated' => array()
        );

        //must follow
        $openid = $this->getOpenID();
        if( !$openid ){
            $result['success'] = false;
            $result['openid'] = null;

            $this->ajaxReturn( $result );
            return;
        }else{
            $result['openid'] = $openid;
        }

        $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
        $result['status'] = $wechat->getFollowStatus( $result['openid'] );
        if( !$result['status'] )
        {
            $result['success'] = false;
            $this->ajaxReturn( $result );
            return;
        }

        //$ruleCheck = D( 'EventRule', 'Logic' );
        $rule = array(
            'day_num' => 4,//每天可以给多少人投票
            'days_num' => -1,//
            'per_day_num' => 1,//每天可以给一个人投几票
            'per_days_num' => 1//活动期间内，可以给一个人重复投几次
        );

        $check = new \Wap\Logic\EventRuleLogic( $rule );
        $left = array();
        $left['day_num'] = $check->isGoodDayNum( $event, $openid );
        $left['per_day_num'] = $check->isGoodPerDayNum( $organiztion, $openid );
        $left['per_days_num'] = $check->isGoodPerDaysNum( $organiztion, $openid );
        if( !$left['day_num'] || !$left['per_day_num'] || !$left['per_days_num'] || $left['day_num'] <= 0 ){
            $result['rule'] = $left;
            $this->ajaxReturn( $result );
            return;
        }else{
            $result['rule'] = $left;
        }

        $praised = D( 'event' )->praiseInc( $event, $organiztion, $openid );

        if( !$praised ){
            $result['success'] = false;
            $this->ajaxReturn( $result );
            return;
        }else{
             $result['success'] = true;
        }

        $result['updated'] = D( 'event' )->rank( $organiztion );

        $this->ajaxReturn( $result );
    }

    public function organiztion()
    {
        $organiztion = I( 'get.id', -1, '' );

        $openid = $this->getOpenID();

        if( !isset( $_GET['code'] ) ){
            $uri = sprintf( "http://%s%s", $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] );
            $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
            $url = $wechat->buildOauthURI( $uri );
            trace( $url, 'url', 'INFO' );
            redirect( $url );
            exit();
        }else if( isset( $_GET['code'] ) ){
            $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
            $openid = $wechat->snsapiBaseUser( $_GET['code'] );
            $this->openid = $openid;
            $this->setOpenID( $this->openid );
        }

        if( !$openid )
        {
            $uri = sprintf( "http://%s%s", $_SERVER['HTTP_HOST'], '/Wap/Event/organiztion/id/'.$organiztion );
            $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
            $url = $wechat->buildOauthURI( $uri );
            trace( $url, 'url', 'INFO' );
            redirect( $url );
            exit();
        }

        $rule = array(
            'day_num' => 4,//每天可以给多少人投票
            'days_num' => -1,//
            'per_day_num' => 1,//每天可以给一个人投几票
            'per_days_num' => 1//活动期间内，可以给一个人重复投几次
        );

        $conditions = array(
            'id' => $organiztion
        );
        $event = M( 'organiztion' )->where( $conditions )->getField( 'event_id' );
        $check = new \Wap\Logic\EventRuleLogic( $rule );
        $passed = false;
        $left = array();
        $left['day_num'] = $check->isGoodDayNum( $event, $openid );
        $left['per_day_num'] = $check->isGoodPerDayNum( $organiztion, $openid );
        $left['per_days_num'] = $check->isGoodPerDaysNum( $organiztion, $openid );
        if( !$left['day_num'] || !$left['per_day_num'] || !$left['per_days_num'] || $left['day_num'] <= 0 ){
            $passed = false;
        }else{
            $passed = true;
        }

        $result = D( 'event' )->rank( $organiztion );
        $result['openid'] = $this->openid;

        //dump( $result );
        $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
        $wx = $wechat->buildWXJSApi();
        $wx['appid'] = $this->appid;

        $this->assign( 'wx', $wx );
        $this->assign( 'passed', $passed );
        $this->assign( 'result', $result );
        $this->display( 'detail' );
    }

    public function organiztions()
    {
        if( IS_GET ){
            $event = I( 'get.event', -1, '' );
            $conditions = array(
                'event_id' => $event
            );
            $result = M( 'organiztion' )
                ->where( $conditions )
                ->order( 'score desc, praise desc' )
                ->select();

            $wechat = new \Wap\Service\WechatService( $this->appid, $this->appsecret );
            $wx = $wechat->buildWXJSApi();
            $wx['appid'] = $this->appid;

            $this->assign( 'wx', $wx );
            $this->assign( 'result', $result );
            $this->display( 'index' );

        }else if( IS_POST ){

            $event = I( 'post.event', 1, '' );
            $keywords = I( 'post.keywords', '', '' );

            $where['name'] = array( 'like', '%'.$keywords.'%' );
            $where['member'] = array( 'like', '%'.$keywords.'%' );
            $where['school'] = array( 'like', '%'.$keywords.'%' );
            $where['_logic'] = 'or';

            trace( $_POST, '搜索表单POST', 'SQL' );
            trace( $_GET, '搜索表单GET', 'SQL' );
            trace( $keywords, '搜索字段', 'SQL' );
            trace( $where, '搜索条件', 'SQL' );
            $conditions['_complex'] = $where;
            $conditions['event_id'] = $event;

            trace( $conditions, '搜索关键字', 'SQL' );

            $result = array();
            if( empty( $keywords ) ){
                $result = M( 'organiztion' )
                    ->where( $conditions )
                    ->order( 'score desc, praise desc' )
                    ->select();
            }else{
                $result = M( 'organiztion' )
                    ->where( $conditions )
                    ->select();
            }

            $this->ajaxReturn( $result );
        }
    }

    private function getOpenID()
    {
        //return "ojTV9tyweV3_OMM9ah29eLMH1FF4";
        return session( $this->appid."_openid" );
    }

    private function setOpenID( $openid )
    {
        session( $this->appid."_openid", $openid );
    }
}
?>
